package org.as3lib.utils
{
	import flash.errors.IllegalOperationError;
	import flash.utils.Dictionary;

	public class CircularDictionary
	{
		private var _dictionary:Dictionary;
		
		public function CircularDictionary(useWeakReferences:Boolean = false) 
		{
			_dictionary = new Dictionary ( useWeakReferences );
		}
		
		public function addPair(a:*, b:*):void {
			if ( _dictionary[a] == undefined && _dictionary[b] == undefined) {
				_dictionary[a] = b;
				_dictionary[b] = a;
				return;
			} 
			throw new IllegalOperationError ("You cannot use the same key or value more than once.");
		}
		
		public function removePair (aOrB:*):* {
			var counterpart:* = _dictionary[aOrB];
			delete _dictionary[aOrB];
			delete _dictionary[counterpart];
			return counterpart;
		}
		
		public function getCounterpartTo(key:*):* {
			return _dictionary[key];
		}
	}
}